package com.nostop.service;

import com.google.common.base.Preconditions;
import com.nostop.common.RequestHolder;
import com.nostop.exception.ParamException;
import com.nostop.mapper.SysAclModuleMapper;
import com.nostop.model.SysAclModule;
import com.nostop.param.AclModuleParam;
import com.nostop.util.BeanValidator;
import com.nostop.util.IpUtil;
import com.nostop.util.LevelUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysAclModuleService {

    @Resource
    private SysAclModuleMapper sysAclModuleMapper;

    public void save(AclModuleParam param) {
        BeanValidator.checkParam(param);
        if (checkExist(param.getParentId(),param.getName(),param.getId())) {
            throw new ParamException("同一层级下存在相同名称的权限模块");
        }
        //创建一个dept类
        SysAclModule sysAclModule = SysAclModule.builder().name(param.getName())
                .parentId(param.getParentId())
                .seq(param.getSeq())
                .status(param.getStatus())
                .remark(param.getRemark())
                .build();
        sysAclModule.setLevel(LevelUtil.calculateLevel(getLevel(param.getParentId()),param.getParentId()));
        sysAclModule.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysAclModule.setOperateId(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysAclModule.setOperateTime(new Date());
        sysAclModuleMapper.insertSelective(sysAclModule);

    }
    public void update(AclModuleParam param) {
        BeanValidator.checkParam(param);
        if (checkExist(param.getParentId(),param.getName(),param.getId())) {
            throw new ParamException("同一层级下存在相同名称的权限模块");
        }
        SysAclModule before = sysAclModuleMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"待更新的权限模块不存在");
        SysAclModule after = SysAclModule.builder().id(param.getId()).name(param.getName())
                .parentId(param.getParentId())
                .seq(param.getSeq())
                .status(param.getStatus())
                .remark(param.getRemark())
                .build();
        after.setLevel(LevelUtil.calculateLevel(getLevel(param.getParentId()), param.getParentId()));
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateId(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());
        updateWithChild(before,after);

    }

    @Transactional
    public void updateWithChild(SysAclModule before, SysAclModule after){
        //判断是否需要更新子部门
        String newLevel = after.getLevel();
        String oldLevel = before.getLevel();
        if(!after.getLevel().equals(before.getLevel())) {
            List<SysAclModule> aclModuleList = sysAclModuleMapper.getChildAclModuleListByLevel(before.getLevel());
            if (CollectionUtils.isNotEmpty(aclModuleList)) {
                for (SysAclModule sysAclModule : aclModuleList) {
                    String level = sysAclModule.getLevel();
                    if (level.indexOf(oldLevel) == 0) {
                        level = newLevel + level.substring(oldLevel.length());
                        sysAclModule.setLevel(level);
                    }
                    sysAclModuleMapper.batchUpdateLevel(aclModuleList);
                }
            }
        }
        sysAclModuleMapper.updateByPrimaryKey(after);
    }

    private boolean checkExist(Integer parentId,String aclModuleName,Integer aclModuleId) {
        //TODO
        return sysAclModuleMapper.countByNameAndParentId(parentId,aclModuleName,aclModuleId) > 0;
    }

    private String getLevel(Integer aclModuleId) {
        SysAclModule sysAclModule = sysAclModuleMapper.selectByPrimaryKey(aclModuleId);
        if (sysAclModule ==null) {
            return null;
        }
        return sysAclModule.getLevel();
    }

}
